package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.JsonRequest;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;


//    @RequiredPermission(name="活动页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


//    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/show")
    public String show(Long id,Model model) {
//        跳转显示选项的页面
        List<ActivityItem> activityItems =activityItemService.selectAllById(id);
        model.addAttribute("actitle",activityService.get(id));
        model.addAttribute("activityitems",activityItems);
        return "activity/page";
    }

    // RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/judge")
    @ResponseBody
    public JsonRequest judge(Long id) {
//        判断当前的用户是否已经投票
        JsonRequest jsonRequest = new JsonRequest();
        Integer count=activityService.selectJudge(UserContext.getUser().getId(),id);
        if (count!=null && count>=1){
            jsonRequest.setSuccess(false);
            jsonRequest.setMsg("您已投票,无法再投!");
        }
        return jsonRequest;
    }

//    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/activists")
    public String gabfests(Long choseId, Long actId, Model model) {
//     点击提交之后,往中间表插入当前员工的id和对应的选项和时间
          activityService.insertChoseId(UserContext.getUser().getId(),choseId);
          activityItemService.updateChoseNum(choseId);
//        获取当前这个活动投票的人数,生成百分比
        List<ActivityItem> list= activityItemService.selectHum(actId);
        model.addAttribute("itemise",list);
        return "activity/pacResult";
    }


    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
        }
        return "activity/input";
    }
}
